package com.itheima.solr.test;

import com.itheima.solr.pojo.Item;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class SolrTestAdvabceQuery {
    /**
     * 通配符查询 ： 类似Lucene  的 WildcardQuery
     *
     * - ？ ： 匹配任意一个字符
     * - *： 匹配任意字符
     */
    @Test
    public void test() throws SolrServerException, IOException {
        //连接服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr/test");
        //创建查询对象
        SolrQuery solrQuery = new SolrQuery("*:*");
        //执行查询，获取响应
        QueryResponse response = solrServer.query(solrQuery);
        //获得查询结果
        SolrDocumentList documentList = response.getResults();

        System.out.println("一共查询到"+documentList.size()+"条数据");
        for (SolrDocument document : documentList) {
            System.out.println("id:"+document.getFieldValue("id"));
            System.out.println("title:"+document.getFieldValue("title"));
            System.out.println("price:"+document.getFieldValue("price"));
        }

        solrServer.commit();
    }

    /**
     * 组合查询：AND ,OR ,NOT  ，类似Lucene的BooleanQuery
     * @throws SolrServerException
     * @throws IOException
     */
    @Test
    public void test2() throws SolrServerException, IOException {
        //连接服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr/test");

        //创建查询对象
//        SolrQuery solrQuery = new SolrQuery("title:汽车 not title:现代");
//        SolrQuery solrQuery = new SolrQuery("title:汽车 and title:手机");
        SolrQuery solrQuery = new SolrQuery("title:汽车 or title:手机");



        //执行查询，获取响应
        QueryResponse response = solrServer.query(solrQuery);
        //获得查询结果
        SolrDocumentList documentList = response.getResults();

        System.out.println("一共查询到"+documentList.size()+"条数据");
        for (SolrDocument document : documentList) {
            System.out.println("id:"+document.getFieldValue("id"));
            System.out.println("title:"+document.getFieldValue("title"));
            System.out.println("price:"+document.getFieldValue("price"));
        }

        solrServer.commit();
    }


    /**
     * 相似度查询： 类似Lucene 的 FuzzyQuery
     *
     * - 语法 ： 把   ~   添加在查询参数后面
     * - 最大编辑距离是 2
     * @throws SolrServerException
     * @throws IOException
     */
    @Test
    public void test3() throws SolrServerException, IOException {
        //连接服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr/test");

        //创建查询对象
        SolrQuery solrQuery = new SolrQuery("title:xiaom~");

        //执行查询，获取响应
        QueryResponse response = solrServer.query(solrQuery);
        //获得查询结果
        List<Item> documentList = response.getBeans(Item.class);

        System.out.println("一共查询到"+documentList.size()+"条数据");
        for (Item document : documentList) {
            System.out.println("id:"+document.getId());
            System.out.println("title:"+document.getTitle());
            System.out.println("price:"+document.getPrice());
        }

        solrServer.commit();
    }

    /**
     * 范围查询： 类似Lucene的 NumericeRangeQuery
     * - 语法:    字段: [x1 TO xn]
     */
    @Test
    public void test4() throws SolrServerException, IOException {
        //连接服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr/test");

        //创建查询对象
        SolrQuery solrQuery = new SolrQuery("id:[1 TO 3]");

        //执行查询，获取响应
        QueryResponse response = solrServer.query(solrQuery);
        //获得查询结果
        List<Item> documentList = response.getBeans(Item.class);

        System.out.println("一共查询到"+documentList.size()+"条数据");
        for (Item document : documentList) {
            System.out.println("id:"+document.getId());
            System.out.println("title:"+document.getTitle());
            System.out.println("price:"+document.getPrice());
        }
    }

    /**
     * 排序:设置排序方式ORDER.desc:降序/ORDER.asc升序
     * @throws SolrServerException
     */
    @Test
    public void test5() throws SolrServerException {
        //连接服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr/test");

        //创建查询对象
        SolrQuery solrQuery = new SolrQuery("id:[1 TO 6]");

        //设置排序方式desc:降序/asc升序
        solrQuery.setSort("id",SolrQuery.ORDER.desc);
        //执行查询，获取响应
        QueryResponse response = solrServer.query(solrQuery);
        //获得查询结果
        List<Item> documentList = response.getBeans(Item.class);

        System.out.println("一共查询到"+documentList.size()+"条数据");
        for (Item document : documentList) {
            System.out.println("id:"+document.getId());
            System.out.println("title:"+document.getTitle());
            System.out.println("price:"+document.getPrice());
        }
    }

    /**
     * 分页
     * @throws SolrServerException
     */
    @Test
    public void test6() throws SolrServerException {
        //连接服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr/test");

        //创建查询对象
        SolrQuery solrQuery = new SolrQuery("title:汽车");
        //设置排序方式desc:降序/asc升序
        solrQuery.setSort("id", SolrQuery.ORDER.asc);
        int pageIndex = 2;
        int pageSize = 3;
        int start = (pageIndex - 1) * pageSize;

        //设置查询分页
        solrQuery.setStart(start);
        solrQuery.setRows(pageSize);
        //执行查询，获取响应
        QueryResponse response = solrServer.query(solrQuery);
        //获得查询结果
        List<Item> documentList = response.getBeans(Item.class);

        System.out.println("一共查询到"+documentList.size()+"条数据");
        for (Item document : documentList) {
            System.out.println("id:"+document.getId());
            System.out.println("title:"+document.getTitle());
            System.out.println("price:"+document.getPrice());
        }
    }

    /**
     * 高亮
     * @throws SolrServerException
     */
    @Test
    public void test7() throws SolrServerException {
        //连接服务器
        HttpSolrServer solrServer = new HttpSolrServer("http://127.0.0.1:8080/solr/test");

        //创建查询对象
        SolrQuery solrQuery = new SolrQuery("id:[1 TO 6]");
        //设置排序方式desc:降序/asc升序
        solrQuery.setSort("id", SolrQuery.ORDER.asc);
        int pageIndex = 2;
        int pageSize = 3;
        int start = (pageIndex - 1) * pageSize;
        //设置查询分页
        solrQuery.setStart(start);
        solrQuery.setRows(pageSize);

        //设置高亮格式
        solrQuery.setHighlightSimplePre("<font color='red>");
        solrQuery.setHighlightSimplePost("</font>");

        //设置高亮的字段
        solrQuery.addHighlightField("title");
        //执行查询，获取响应
        QueryResponse response = solrServer.query(solrQuery);
        //获得查询结果
        List<Item> documentList = response.getBeans(Item.class);
        System.out.println("一共查询到"+documentList.size()+"条数据");
        //获取高亮数据
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        //5.遍历查询结果
        for (Item item : documentList){
            System.out.println("id:" + item.getId());
            System.out.println("title:" + item.getTitle());
            System.out.println("price:" + item.getPrice());

            //获取高亮字段的
            Map<String, List<String>> titleMap = highlighting.get(item.getId());
            //获取高亮字段的数据
            List<String> titleList = titleMap.get("title");
            String highlighterTitle = titleList.get(0);
            System.out.println("highlighterTitle:" + highlighterTitle);

        }
    }
}
